<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>
        后台管理系统
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/assets/admin/css/font.css}">
    <link rel="stylesheet" th:href="@{/assets/admin/css/xadmin.css}">
    <script th:src="@{/assets/admin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/assets/admin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style type="text/css">
        .layui-form-switch {
            margin-top: 8px;
        }

    </style>
</head>
<body>
<div class="x-nav">
           <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>系统管理</cite></a>
              <a><cite>菜单管理</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div id="menu_tree">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form" lay-filter="menu-form">
                        <input type="hidden" name="id" value="" />
                        <div class="layui-form-item">
                            <label for="parentId" class="layui-form-label">
                                <span class="x-red">*</span>所在目录名
                            </label>
                            <div class="layui-input-inline">
                                <select id="parentId" name="parentId">
                                    <option value="">请选择所在目录</option>
                                    <option th:each="menu:${menus}" th:value="${menu.id}"
                                            th:text="${menu.menuname}"></option>
                                </select>
                            </div>
                            <div class="layui-form-mid layui-word-aux">
                                <span class="x-red">*</span>所属父菜单，如果作为一级父菜单，不用选择
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label for="menuname" class="layui-form-label">
                                <span class="x-red"></span>菜单名
                            </label>
                            <div class="layui-input-block">
                                <input type="text" id="menuname" name="menuname" required="" lay-verify="required"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label for="iconCode" class="layui-form-label">
                                菜单图标
                            </label>
                            <div class="layui-input-inline">
                                <input type="text" id="iconCode" name="iconCode"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label for="menuCode" class="layui-form-label">
                                <span class="x-red">*</span>菜单编号
                            </label>
                            <div class="layui-input-inline">
                                <input type="text" id="menuCode" name="menuCode" required="" lay-verify="required"
                                       autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid layui-word-aux">
                                <span class="x-red">*</span>唯一标识
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label for="sortNo" class="layui-form-label">
                                <span class="x-red">*</span>排序
                            </label>
                            <div class="layui-input-block">
                                <input type="text" id="sortNo" name="sortNo" required="" lay-verify="required"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label for="url" class="layui-form-label">
                                <span class="x-red">*</span>请求路径
                            </label>
                            <div class="layui-input-inline">
                                <input type="text" id="url" name="url" required="" lay-verify="required"
                                       autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid layui-word-aux">
                                <span class="x-red">*</span>不包含中文与特殊字符
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="status" id="status" lay-skin="switch" lay-text="开启|关闭"
                                       lay-filter="switchTest">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn" name="addBtn" lay-filter="add" lay-submit="">
                                增加
                            </button>
                            <button class="layui-btn" name="updateBtn" lay-filter="update" lay-submit="">
                                更新
                            </button>
                            <button class="layui-btn" name="resetBtn" lay-filter="reset-ot">
                                清空
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="layui-container">
</div>
</body>
<script th:replace="systemCommon/HarinCommon :: commonJs"></script>
<script>
    let currentData= null;
    layui.use(["laydate", "form", "layer", "tree","jquery"], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        layer = layui.layer;//弹出层
        tree = layui.tree;
        form = layui.form;



        tree_obj = {
            elem: '#menu_tree',
            data: null,
            id: 'id',
            showCheckbox: false,      //是否显示复选框
            onlyIconControl: true,    //有单击事件，所以这个应该关闭
            showLine: true,           //是否开启连接线。默认 true，若设为 false，则节点左侧出现三角图标。
            edit: ['del'],              //是否开启节点的操作图标
            accordion: true,
            text: {
                defaultNodeName: '未命名',     //节点默认名称
                none: '无数据'                 //数据为空时的提示文本
            },
            click: function (obj) {             //单击事件
                //console.log(obj.data);          //得到当前点击的节点数据
                //console.log(obj.state);         //得到当前节点的展开状态：open、close、normal
                //console.log(obj.elem);          //得到当前节点元素
                //console.log(obj.data.children); //当前节点下是否有子节点
                $.ajax({
                    url: "/system/getMenuById",
                    type: "post",
                    async: false,
                    data: {"id": obj.data.id},
                    dataType: "JSON",
                    success: function (result) {
                        var data = result.result;
                        currentData = data;
                        //console.log(data);
                        form.val("menu-form", {
                            "id": data.id,
                            "menuname": data.menuname,
                            "iconCode":data.iconCode,
                            "menuCode": data.menuCode,
                            "sortNo": data.sortNo,
                            "url": data.url
                        });
                        //调用改变的方法
                        $("input[name='id']").change();
                        $("#parentId option").removeAttr("selected");
                        let parentId = isEmpty(data.parentId)?"":data.parentId;
                        $("#parentId").children("option").each(function () {
                            var item = $(this);
                            //console.log(item.val());
                            if (item.val() == parentId) {
                                //这里不能使用attr方法，会出现两次正常回显后失效问题
                                item.prop("selected", true);
                            }
                        });

                        //$("#parentId option[value='"+data.parentId+"']").attr("selected",'true');
                        $("#status").prop("checked", data.status == 1);
                        form.render();
                    }

                });

            },
            operate: function (obj) {       //操作
                var type = obj.type; //得到操作类型：add、edit、del
                var data = obj.data; //得到当前节点的数据
                var elem = obj.elem; //得到当前节点元素

                //Ajax 操作
                var id = data.id; //得到节点索引
                if (type === 'add') { //增加节点
                    //返回 key 值
                    return 123;
                } else if (type === 'update') { //修改节点
                    //console.log(elem.find('.layui-tree-txt').html()); //得到修改后的内容
                } else if (type === 'del') { //删除节点
                    //console.log(id + "被删除了")
                    console.log(type);
                    console.log(data);
                    console.log(elem);
                    $.ajax({
                        url: "/system/deleteMuneByid",
                        type:"delete",
                        data:{"menuId":data.id},
                        success:function(data){
                            if (data.code == 200) {
                                layer.alert(data.message, {icon: 6}, function (index) {
                                    layer.close(index);
                                });
                            } else {
                                layer.alert(data.message, {icon: 5}, function (index) {
                                    layer.close(index);
                                });
                            }
                        }
                    })
                };
            }
        }

        menuTree = tree.render(tree_obj);

        //以上模块根据需要引入
        // tree.render(tree_obj);
        getData();
        //监听提交
        form.on('submit(add)', function (data) {
            //console.log(typeof data.field);
            var status = data.field.status;
            if ("on" == status) {
                data.field.status = 1;
            } else {
                data.field.status = 0;
            }
            //console.log(data);
            //发异步，把数据提交给后台处理
            $.ajax({
                url: "/system/addMenu",
                data: JSON.stringify(data.field),
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    //console.log(data.message);
                    if (data.code == 200) {
                        layer.alert(data.message, {icon: 6}, function (index) {
                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            layer.close(index);
                        });
                        getData();
                    } else {
                        layer.alert(data.message, {icon: 5}, function (index) {
                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            layer.close(index);
                        });
                    }
                }
            });
            return false;
        });
        form.on('submit(update)', function (data) {
            //console.log(typeof data.field);
            var status = data.field.status;
            data.field.id = new Number(data.field.id);
            if ("on" == status) {
                data.field.status = 1;
            } else {
                data.field.status = 0;
            }
            if(isEmpty(data.field.parentId)){
                data.field.level = 1;
                data.field.parentId = 0;
            }else{
                data.field.level = 0;
            }
            //console.log(data);
            //发异步，把数据提交给后台处理
            $.ajax({
                url: "/system/updateMuneById",
                data: JSON.stringify(data.field),
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    //console.log(data.message);
                    if (data.code == 200) {
                        layer.alert(data.message, {icon: 6}, function (index) {
                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            layer.close(index);
                        });
                        getData();
                    } else {
                        layer.alert(data.message, {icon: 5}, function (index) {
                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            layer.close(index);
                        });
                    }
                }
            });
            return false;
        });
        form.on("submit(reset-ot)",function(data){
            form.val("menu-form", {
                "id": "",
                "menuname":  "",
                "menuCode":  "",
                "sortNo":  "",
                "url":  ""
            });
            return false;
        });

        $("input[name='id']").on('change',function(event){
            //console.log(123);
            $("button[name='addBtn']").hide();
            $("button[name='updateBtn']").show();
        });
    });


    //批量删除提交
    function delAll() {
        layer.confirm('确认要删除吗？', {icon: 3, title: '提示信息'}, function (index) {
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
        });
    }

    /*添加*/
    function admin_add(title, url, w, h) {
        x_admin_show(title, url, w, h);
    }

    /*停用*/
    function admin_stop(obj, id) {
        layer.confirm('确认要停用吗？', function (index) {
            //发异步把用户状态进行更改
            $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="admin_start(this,id)" href="javascript:;" title="启用"><i class="layui-icon">&#xe62f;</i></a>');
            $(obj).parents("tr").find(".td-status").html('<span class="layui-btn layui-btn-disabled layui-btn-mini">已停用</span>');
            $(obj).remove();
            layer.msg('已停用!', {icon: 5, time: 1000});
        });
    }

    /*启用*/
    function admin_start(obj, id) {
        layer.confirm('确认要启用吗？', function (index) {
            //发异步把用户状态进行更改
            $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="admin_stop(this,id)" href="javascript:;" title="停用"><i class="layui-icon">&#xe601;</i></a>');
            $(obj).parents("tr").find(".td-status").html('<span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span>');
            $(obj).remove();
            layer.msg('已启用!', {icon: 6, time: 1000});
        });
    }

    //编辑
    function admin_edit(title, url, id, w, h) {
        x_admin_show(title, url, w, h);
    }

    /*删除*/
    function admin_del(obj, id) {
        layer.confirm('确认要删除吗？', {icon: 3, title: '提示信息'}, function (index) {
            //发异步删除数据
            $(obj).parents("tr").remove();
            layer.msg('已删除!', {icon: 1, time: 1000});
        });
    }

    /*获取后台的菜单数据*/
    function getData() {
        //layui.use(['tree'], function () {
            //tree = layui.tree;
            $.ajax({
                url: "/system/getMenu",    //后台数据请求地址
                type: "post",
                async: false,
                success: function (resut) {
                    res_data = resut.result;
                    tree_obj.data = res_data;
                    menuTree.reload(tree_obj);
                }
            });
        //});
    }
</script>
</html>